﻿#ifndef Dir_hpp__2013_7_30__12_35
#define Dir_hpp__2013_7_30__12_35

df::CC  df::GetDirCurrent()
{
	String & menu = getCurrentDirString();

	static intptr_t len = -1;


	if (menu.size()<1)
	{
		menu.resize(df::BUFFER_SIZE);
#ifdef WIN32
		
		///获取模块路径
		len = ::GetModuleFileName(0, &menu[0],(DWORD) menu.capacity());
		menu.resize(len);

		auto pos = CC::Find(menu, _T('\\'), len - 1, false);
		if (pos >= 0)
		{
			len = pos + 1;
			menu.resize(len);
			//progressDir_[len] = 0;
		}

#else
		if (getcwd(&menu[0], menu.capacity() - 2))
		{
			len = strlen(&menu[0])+1;
			menu[len-1]='/';
			//progressDir_[len-1]='/';
			menu.resize(len);
		}

#endif // WIN32
	}


	return df::CC(menu.c_str(), menu.size());
}


df::DirRes::DirRes(const df::CC & menu, const df::CC & filter)
{
	has_ = true;
#ifdef WIN32
	intptr_t filterSize = (filter.length_ + 1) * sizeof(TCHAR);
	intptr_t menuSize = (menu.length_ + 1) * sizeof(TCHAR);
	////文件信息
	memBuf = new char[sizeof(WIN32_FIND_DATA)+menuSize + filterSize];
	buf = (WIN32_FIND_DATA *)memBuf;
	//组合文件目录与通配符
	TCHAR * newMenu = (TCHAR *)(buf + 1);
	memcpy(newMenu, menu.char_, menuSize);
	newMenu[menu.length_] = _T('\\');
	memcpy((char*)newMenu + menuSize, filter.char_, filterSize);


	//获取第一个文件
	hSearch = FindFirstFile(newMenu, buf);
	if (hSearch == INVALID_HANDLE_VALUE)
		has_ = false;
	if (hSearch == NULL)
	{
		DF_ERR(menu << tcc_("FindFirstFile faild,filter:") << filter);
		has_ = false;
	}
	
#else
	//struct stat statbuf;
	if ((dp = opendir(menu.char_)) == NULL) {
		has_ = false;
	}
#endif
}




template<class LamT>
int df::EachDisk(LamT lam)
{
	int count = 0;
#ifdef WIN32/*
	//获取所有驱动器字符串信息长度。
	int length = ::GetLogicalDriveStrings(0, NULL);
	if (length == 0)
	{
	ERR(tcc_("GetLogicalDriveStrings faild"));
	return 0;
	}
	df::SS diskStrBuf(length);
	TCHAR * diskStr = diskStrBuf.GetBuffer();
	if (::GetLogicalDriveStrings(length, diskStr) == 0)
	{
	ERR(tcc_("GetLogicalDriveStrings faild"));
	return 0;
	}

	df::DiskInfo info = { { 0 } };
	info.name_[1] = tt_(':');
	unsigned __int64 i64FreeBytes;

	for (int i = 0; i < length / 4; i++)
	{
	info.name_[0] = diskStr[i * 4];
	info.name_[2] = 0;
	info.type_ = ::GetDriveType(diskStr + i * 4);
	info.totalSize_ = 0;
	info.freeSize_ = 0;

	///获取驱动器磁盘的空间状态
	if (!::GetDiskFreeSpaceEx(diskStr + i * 4, (PULARGE_INTEGER)&(info.freeSize_), (PULARGE_INTEGER)&(info.totalSize_), (PULARGE_INTEGER)&i64FreeBytes))
	{
	//ERROR
	}
	count++;

	lam(info);

	}*/
#endif
	return count;
}


#endif // Dir_hpp__2013_7_30__12_35
